package com.moyu.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.moyu.common.core.contants.HttpConstants;
import com.moyu.common.core.domain.LoginUser;
import com.moyu.common.core.domain.R;
import com.moyu.common.security.service.TokenService;
import com.moyu.user.domain.feedback.FeedBack;
import com.moyu.user.domain.feedback.dto.FeedBackInsertDTO;
import com.moyu.user.domain.feedback.dto.FeedBackQueryDTO;
import com.moyu.user.domain.feedback.dto.FeedBackUpdateDTO;
import com.moyu.user.domain.feedback.vo.FeedBackVO;
import com.moyu.user.mapper.FeedBackMapper;
import com.moyu.user.service.IFeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 反馈服务实现类
 *
 * @Author 赵彦博
 * @Date 2025/4/19
 */
@Service
public class FeedBackServiceImpl implements IFeedBackService {

    @Autowired
    private FeedBackMapper feedBackMapper;

    @Autowired
    private TokenService tokenService;

    @Value("${jwt.secret}")
    private String secret;

    @Override
    public int createFeedBack(String token, FeedBackInsertDTO feedBackInsertDTO) {
        if (StrUtil.isNotEmpty(token) && token.startsWith(HttpConstants.PREFIX)) {
            token = token.replaceFirst(HttpConstants.PREFIX, StrUtil.EMPTY);
        }
        LoginUser loginUser = tokenService.getLoginUser(token, secret);
        String userId = loginUser.getUserId();
        FeedBack feedBack = new FeedBack();
        feedBack.setType(feedBackInsertDTO.getType());
        feedBack.setContent(feedBackInsertDTO.getContent());
        feedBack.setContact(feedBackInsertDTO.getContact());
        feedBack.setUserId(Long.valueOf(userId));
        feedBack.setStatus(0);
        return feedBackMapper.insert(feedBack);
    }

    @Override
    public List<FeedBackVO> list(FeedBackQueryDTO feedBackQueryDTO) {
        PageHelper.startPage(feedBackQueryDTO.getPageNum(), feedBackQueryDTO.getPageSize());
        return feedBackMapper.selectFeedBackList(feedBackQueryDTO);
    }

    @Override
    public int process(FeedBackUpdateDTO feedBackUpdateDTO) {
        return feedBackMapper.process(feedBackUpdateDTO);
    }
}
